<template>
<d2-container>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
           style="width: 30%;">
    <el-form-item label="总数" prop="total">
      <el-input v-model="ruleForm.total"></el-input>
    </el-form-item>
    <!--    <el-form-item label="产品类型" prop="productType">-->
    <!--      <el-input v-model="ruleForm.productType"></el-input>-->
    <!--    </el-form-item>-->
    <el-form-item label="产品类型" prop="productType">
      <el-select v-model="ruleForm.productType" placeholder="请选择产品类型">
        <el-option v-for="item in ruleForm.product_category" :label="item" :value="item"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="所需材料" prop="material">
      <el-select v-model="ruleForm.material" multiple placeholder="请选择">
        <el-option
          v-for="item in ruleForm.material_category"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="ruleForm.remark"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>

</d2-container>
</template>

<script>
const axios = require('axios');
import {request} from "@/network/request";

export default {
  name: 'create',
  data() {
    return {
      ruleForm: {
        total: null,
        productType: null,
        material_category: null,
        material: null,
        remark: "无",
        product_category: null
      },
      rules: {
        total: [
          {required: true, message: '请输入需要该批订单商品数量', trigger: 'blur'}
        ],
        productType: [
          {required: true, message: '请输入该批产品类型', trigger: 'blur'}
        ],
        material_category: [
          {required: true, trigger: 'blur'}
        ],
        remark: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      let _this = this;
      let material = this.ruleForm.material[0];
      for (let i = 1; i < this.ruleForm.material.length; i++) {
        material += "," + this.ruleForm.material[i];
      }
      console.log("material:" + material);
      //上传
      request({
          url: 'batch/add',
          method: 'post',
          params: {
            total: _this.ruleForm.total,
            productType: _this.ruleForm.productType,
            material: material,
            remark: _this.ruleForm.remark
          }
        },
        res => {
          alert("添加成功！");
          console.log(res);
        },
        err => {
          alert("添加失败，请重试！");
          console.log(err);
        }
      );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    let _this = this;
    //原材料
    request({
        url: 'material/query',
        params: {
          key: '',
          pages: 1,
          num: 100
        },
      },
      res => {
        _this.ruleForm.material_category = [];
        for (let i = 0; i < res.data.records.length; i++) {
          _this.ruleForm.material_category[i] = {};
          _this.ruleForm.material_category[i].label = res.data.records[i].alias;
          _this.ruleForm.material_category[i].value = res.data.records[i].id;
        }
      },
      err => {
        console.log(err);
      });

    //商品分类
    request({
        url: 'productCategory/query',
        params: {
          key: '',
          pages: '1',
          num: 100
        },
      },
      res => {
        _this.ruleForm.product_category = [];
        for (let i = 0; i < res.data.records.length; i++) {
          _this.ruleForm.product_category.push(res.data.records[i].name);
        }
      },
      err => {
        console.log(err);
      })
  }
}
</script>
